﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Web;

namespace blackhouse.Aparts.Web.Pages {
	public partial class ConfirmReservation : NonCmsBasePage {

		#region Fields

		private int reservationId;
		private Guid guid;
		private IUserApartmentsReservation reservation;

		#endregion

		#region Event Handlers

		protected override void OnPreInit(EventArgs e) {
			this.AllowSkinOverride = true;
			HttpContext.Current.Items.Add("skin", "blackhouse");

			base.OnPreInit(e);
		}

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			this.ReadParameters();
			this.SetInstance();
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.PopulateControls();
		}

		#endregion

		#region Methods

		private void ReadParameters() {
			Int32.TryParse(this.Request["id"], out this.reservationId);
			Guid.TryParse(this.Request["reservation"], out this.guid);
		}

		private void SetInstance() {
			this.reservation = Factory.Instance().GetUserApartmentsReservation(this.reservationId);
			if (this.reservation != null && (!this.reservation.IsAuthentic(this.guid))) // sprawdzenie czy guid jest właściwy i czy status jest: rezerwacja wstępna - czeka na potwierdzenie
				this.reservation = null;
			if (this.reservation != null && !this.reservation.ChangeStatusTo(14))
				this.reservation = null;
		}

		private void PopulateControls() {
			if (this.reservation == null) {
				this.pnlOk.Visible = false;
				return;
			}
			this.lblReservationNumber.Text = this.reservation.ReservationNumber;
			this.lnkDisplayReservation.NavigateUrl = UrlUtils.LocalizeUrl(Factory.Instance().GetPreFolderEvaluation(), "/pages/displayreservation.aspx?number=" + this.reservation.ReservationNumber);
			this.lnkDisplayReservation.Text = string.Format(GetGlobalResourceObject("Aparts", "confirmBooking4").ToString(), this.reservation.ReservationNumber);
		}

		#endregion

	}
}